﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using QUANLYCHAMCONG.BL.BUS;
using DgvFilterPopup;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace QUANLYCHAMCONG.PL.CMS.Nhanvien
{
    public partial class LichLamviec : UserControl
    {
        public delegate void _Close();
        public event _Close Close;
        STAFF nv;
        LICHLAMVIEC lich;
        ClsLICHLAMVIECCollection dslich;
        string MAPB;
        string MACV;
        int flag;
        frmHoidap f;
        public LichLamviec()
        {
            InitializeComponent();
        }
        private void cmbpb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbpb.SelectedValue.ToString() != "System.Data.DataRowView" && cmbpb.SelectedValue.ToString() != "")
            {
                MAPB = this.cmbpb.SelectedValue.ToString();
                cmbcv.Enabled = true;
                if (cmbcv.Enabled == true)
                {
                    Nhanvien();
                }
            }
        }
        private void cmbcv_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbcv.SelectedValue.ToString() != "System.Data.DataRowView" && cmbcv.SelectedValue.ToString() != "")
            {
                MACV = this.cmbcv.SelectedValue.ToString();
                cbnv.Enabled = true;
                cmbca.Enabled = true;
                Nhanvien();
            }
        }
        void Nhanvien()
        {
            nv = new STAFF();
            nv.MACV = this.MACV;
            nv.MAPB = this.MAPB;
            this.cbnv.Properties.DataSource = STAFFBUS.SelectbyGroupPB_CV(nv);
            this.cbnv.Properties.DisplayMember = "TENNV";
            this.cbnv.Properties.ValueMember = "MANV";

        }
        private void Event()
        {
            this.toolbar1.Add += new Toolbar._Add(toolbar1_Add);
            this.toolbar1.Save += new Toolbar._Save(Save);
            this.toolbar1.Delete += new Toolbar._Delete(toolbar1_Delete);
            this.toolbar1.Close += new Toolbar._Close(toolbar1_Close);
            this.toolbar1.Refresh += new Toolbar._Refesh(Refresh);
            InitGrid();
            LoadData();
        }
        void Refresh()
        {
            RemoveCheckbox();
            this.toolbar1.SetSave();
            this.flag = 0;
            this.dgvdsnv.ReadOnly = true;
            this.dgvdsnv.AllowUserToAddRows = true;
            LoadData();
            this.Clear();
        }
        void Save()
        {
            bool IsCommplete = true;
            dslich = new ClsLICHLAMVIECCollection();
            if (flag == 1)//them nhan vien
            { 
                String []ca =new String[this.cmbca.Properties.Items.Count];
                int i=0;
                foreach (CheckedListBoxItem item in this.cmbca.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        ca[i] = item.Value.ToString();
                        i++;
                    }
                }
                //copy ca sang mang moi
                string[] ca1 = new string[i];
                for (int j = 0; j < i; j++)
                {
                    ca1[j] = ca[j];
                }
                //thuc hien copy nhan vien
                string maca=null;
                for(int q=0;q<ca1.Length;q++)
                {
                    maca=ca1[q];
                    foreach(CheckedListBoxItem it in this.cbnv.Properties.Items)
                    {
                        if (it.CheckState == CheckState.Checked)
                        {
                            lich = new LICHLAMVIEC();
                            lich.MANV = it.Value.ToString();
                            lich.MACA = maca;
                            DataTable dt = new DataTable();
                            dt = LICHLAMVIECBUS.SelectPK(lich);
                            if (dt.Rows.Count != 0)
                            {
                                f = new frmHoidap("Nhân viên " + lich.MANV.ToString() + " đă đăng ký ca!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                                f.ShowDialog();
                                IsCommplete = false;
                                continue;
                            }
                            dslich.Add(lich);
                        }
                       
                    }
                }
                
            }
            if (IsCommplete == true && flag == 1)
            {
                bool Commplete = true;
                int soluong = 0;
                foreach (LICHLAMVIEC lich in dslich)
                {
                    Commplete = LICHLAMVIECBUS.Insert(lich);
                    if (Commplete)
                        soluong++;
                }
                if (Commplete)
                {
                    f = new frmHoidap("Đăng ký ca thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    LoadData();
                    Clear();
                    SetToolbar();
                }
                else
                {
                    int sodele = 0;
                    foreach (LICHLAMVIEC lich in dslich)
                    {
                        LICHLAMVIECBUS.Delete(lich);
                        sodele++;
                        if (soluong == sodele)
                            break;
                    }
                    f = new frmHoidap("Đăng ký ca thất bại!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    Clear();
                    SetToolbar();
                }
            }
            if (flag == 3)
            {
                bool kq = true;
                f = new frmHoidap("Bạn có chắc muốn xóa các đăng ký vừa chọn không?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.canhbao);
                if (f.ShowDialog() == DialogResult.Yes)
                {
                    try
                    {

                        foreach (DataGridViewRow dr in this.dgvdsnv.Rows)
                        {
                            if (dr.Cells[0].Value != null)
                            {
                                lich = new LICHLAMVIEC();
                                lich.MACA = dr.Cells["MACA"].Value.ToString();
                                lich.MANV = dr.Cells["MANV"].Value.ToString();
                                kq = LICHLAMVIECBUS.Delete(lich);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        f = new frmHoidap("Có lỗi " + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                        f.ShowDialog();
                        SetToolbar();
                    }
                    if (kq)
                    {
                        f = new frmHoidap("Xóa thành công lịch làm việc!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                        f.ShowDialog();
                        RemoveCheckbox();
                        LoadData();
                        this.dgvdsnv.ReadOnly = true;
                        this.dgvdsnv.AllowUserToAddRows = true;
                        Clear();
                        SetToolbar();
                    }
                }
            }
        }
        void toolbar1_Close()
        {
            if (this.Close != null)
                this.Close();
        }
        private int FindAviable()
        {
            foreach (DataGridViewColumn dr in this.dgvdsnv.Columns)
            {
                if (dr is DataGridViewCheckBoxColumn)
                    return 1;
            }
            return 0;
        }
        private void RemoveCheckbox()
        {
            if (FindAviable() == 1)
            {
                this.dgvdsnv.Columns.RemoveAt(0);
                this.dgvdsnv.Controls.RemoveByKey(((CheckBox)this.dgvdsnv.Controls.Find("checkboxHeader", true)[0]).Name);
                LoadData();
            }
        }
        private void AddCheckbox(DataTable dt)
        {
            if (FindAviable() == 0)
            {
                DataGridViewCheckBoxColumn cb = new DataGridViewCheckBoxColumn();
                cb.Width = 30;
                cb.FillWeight = 30;
                cb.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.dgvdsnv.Columns.Insert(0, cb);
                Rectangle rect = this.dgvdsnv.GetCellDisplayRectangle(0, -1, true);
                rect.X = rect.Location.X + cb.Width / 33;
                CheckBox checkboxHeader = new CheckBox();
                checkboxHeader.Name = "checkboxHeader";
                checkboxHeader.Size = new Size(18, 18);
                checkboxHeader.Location = rect.Location;
                checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
                this.dgvdsnv.Controls.Add(checkboxHeader);
                this.dgvdsnv.Columns[0].Width = 50;
                this.dgvdsnv.DataSource = dt;
            }
        }
        void checkboxHeader_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)this.dgvdsnv.Controls.Find("checkboxHeader", true)[0]).Checked == true)
            {
                for (int i = 0; i < this.dgvdsnv.RowCount; i++)
                {
                    this.dgvdsnv[0, i].Value = ((CheckBox)this.dgvdsnv.Controls.Find("checkboxHeader", true)[0]).Checked;
                }
            }
            else
            {
                for (int i = 0; i < this.dgvdsnv.RowCount; i++)
                {
                    this.dgvdsnv[0, i].Value = false;
                }
            }
        }
        void toolbar1_Delete()
        {
            flag = 3;
            this.dgvdsnv.ReadOnly = false;
            this.dgvdsnv.AllowUserToAddRows = false;
            this.toolbar1.SetAdd_Edit_Delete();
            AddCheckbox(LICHLAMVIECBUS.SelectAll());

        }
        void toolbar1_Add()
        {
            flag = 1;
            Clear();
            this.toolbar1.SetAdd_Edit_Delete();
            this.cmbpb.Enabled = true;

        }
        void LoadData()
        {
            try
            {
                Common.HightLightDataGridViewRow(ref this.dgvdsnv, LICHLAMVIECBUS.SelectAll());
                SetGrid();
                BindCombo();
            }
            catch
            { }
        }
        void SetGrid()
        {
            this.dgvdsnv.Columns["MACA"].Visible = false;
        }
        void SetToolbar()
        {
            this.toolbar1.SetToolBar(true, false, true, false, true, false, false, false);
        }
        void BindCombo()
        {

            this.cmbpb.DataSource = PHONGBANBUS.SelectAll();
            this.cmbpb.DisplayMember = "TENPB";
            this.cmbpb.ValueMember = "MAPB";
            this.cmbcv.DataSource = CHUCVUBUS.SelectAll();
            this.cmbcv.DisplayMember = "TENCV";
            this.cmbcv.ValueMember = "MACV";
            this.cmbca.Properties.DataSource = SHILFBUS.SelectAll();
            this.cmbca.Properties.ValueMember = "MACA";
            this.cmbca.Properties.DisplayMember = "TENCA";
        }
        private void InitGrid()
        {
            DgvFilterManager f = new DgvFilterManager();
            f.ColumnFilterAdding += new ColumnFilterEventHandler(f_ColumnFilterAdding);
            f.DataGridView = (DataGridView)this.dgvdsnv;
        }
        void f_ColumnFilterAdding(object sender, ColumnFilterEventArgs e)
        {
            switch (e.Column.Name)
            {
                case "HOTEN":
                case "PHONGBAN":
                case "CHUCVU":
                case "CA":
                case "MANV":
                    e.ColumnFilter = new DgvComboBoxColumnFilter();
                    break;
            }
        }
        private void SetLink()
        {
            this.links1.SetValues("Nhân viên", "Lịch làm việc nhân viên");
        }
        void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is Panel)
                {
                    foreach (Control d in c.Controls)
                    {
                        if (d is TextBox)
                            d.Text = "";
                        if (d is System.Windows.Forms.ComboBox)
                            d.Enabled = false;
                        if (d is CheckedComboBoxEdit)
                        {
                            d.Enabled = false;
                            d.Text = "";
                        }
                    }
                }
            }
        }

        private void LichLamviec_Load(object sender, EventArgs e)
        {
            SetLink();
            Event();
            SetToolbar();
        }
    }
}
